package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Insurancebill;
import cn.wolfcode.crm.query.BillQueryObject;
import cn.wolfcode.crm.query.ObjectQuery;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.OddObjectQuery;
import cn.wolfcode.crm.service.IInsurancebillService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("insurancebill")
public class InsurancebillController {
    @Autowired
    private IInsurancebillService insurancebillService;

    @RequiresPermissions("insurancebill:index")
    @PermissionName("投保单历史记录主页")
    @RequestMapping("index")
    public String index(Model model, @ModelAttribute("qo") ObjectQuery qo) {
        return "insurancebill/list";
    }

    @RequiresPermissions("insurancebill:list")
    @PermissionName("投保单历史记录查询")
    @RequestMapping("list")
    @ResponseBody
    public PageResult<Insurancebill> list(Model model, @ModelAttribute("qo") BillQueryObject qo) {
        PageResult<Insurancebill> list = insurancebillService.query(qo);
        return list;
    }


    @RequiresPermissions("insurancebill:delete")
    @PermissionName("投保单历史记录删除")
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        JsonResult jsonResult = new JsonResult();
        try {
            if (id != null) {
                insurancebillService.delete(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.markErrorMsg("删除投保单历史记录失败");
        }
        return jsonResult;
    }


    @RequiresPermissions("insurancebill:save")
    @PermissionName("投保单历史记录创建更新")
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Insurancebill bill) {
        JsonResult jsonResult = new JsonResult();
        insurancebillService.save(bill);
        return jsonResult;
    }

    /**
     * @param bill
     * @return
     */

    @RequiresPermissions("insurancebill:update")
    @PermissionName("暂存单[保单]更新")
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Insurancebill bill) {
        JsonResult jsonResult = new JsonResult();
        insurancebillService.update(bill);
        return jsonResult;
    }

    /**
     * znb
     *
     * @return 暂存单主页
     */
    @RequiresPermissions("insurancebill:tempOdd")
    @PermissionName("暂存单主页")
    @RequestMapping("tempOdd")
    public String tempOdd() {
        return "temporaryOdd/list";
    }

    /**
     * znb
     *
     * @return 批改单主页
     */
    @RequiresPermissions("insurancebill:modifyOdd")
    @PermissionName("批改单主页")
    @RequestMapping("modifyOdd")
    public String modifyOdd() {
        return "modifyOdd/list";
    }

    /**
     * znb
     *
     * @return 待审核单主页
     */
    @RequiresPermissions("insurancebill:waitAduitOdd")
    @PermissionName("批改单主页")
    @RequestMapping("waitAduitOdd")
    public String waitAduitOdd() {
        return "waitAduitOdd/list";
    }

    /**
     * znb
     * 可筛选查询保险单数据单数据
     *
     * @return
     */
    @RequiresPermissions("insurancebill:queryOdd")
    @PermissionName("查询暂存单")
    @RequestMapping("queryOdd")
    @ResponseBody
    public PageResult<Insurancebill> queryOdd(OddObjectQuery qo) {
        return insurancebillService.query(qo);
    }


    /**
     * znb
     * 更改保险单状态
     *
     * @return
     */
    @RequiresPermissions("insurancebill:changState")
    @PermissionName("更改保险单状态[非驳回待修改]")

    @RequestMapping("changState")
    @ResponseBody
    public JsonResult changState(Long id, int state) {
        return changState(id, state, null);
    }

    /**
     * znb
     * 驳回待修改
     *
     * @return
     */
    @RequiresPermissions("insurancebill:changState2Modify")
    @PermissionName("驳回待修改")

    @RequestMapping("changState2Modify")
    @ResponseBody
    public JsonResult changState(Long id, int state, String info) {
        JsonResult jsonResult = new JsonResult();
        try {
            if (id != null) {
                insurancebillService.changState(id, state,info);
            }

        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.markErrorMsg("更改暂存单状态失败");
        }
        return jsonResult;
    }

    @RequestMapping("checkWhichCompensate")
    @ResponseBody
    public List<Insurancebill> checkWhichCompensate(Long id) {
        List<Insurancebill>  bill = new ArrayList<>();
        bill = insurancebillService.checkWhichCompensate(id);
        return bill;
    }


}
